package com.spring.nbrbt.tools.otherutil;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.List;
import java.util.UUID;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.factory.annotation.Value;

public class NbUtil {
	
	static InetAddress netAddress = null;
	
    static {  
        try{  
        	netAddress = InetAddress.getLocalHost();  
        }catch(UnknownHostException e){  
            System.out.println("unknown host!");  
        }  
    }
	
	public static String getHostIp(){  
        if(null == netAddress){  
            return null;  
        }  
        String ip = netAddress.getHostAddress();
        return ip;  
    }  
  
    public static String getHostName(){  
        if(null == netAddress){  
            return null;  
        }  
        String name = netAddress.getHostName();
        return name;  
    }
	
	/**
	 * 产生一个36个字符的UUID
	 * 
	 * @return UUID
	 */
	public static String randomUUID() {
		return UUID.randomUUID().toString();
	}
	
	/**
	 * md5加密
	 * 
	 * @param value
	 *            要加密的值
	 * @return md5加密后的值
	 */
	public static String md5Hex(String value) {
		return DigestUtils.md5Hex(value);
	}

	/**
	 * sha1加密
	 * 
	 * @param value
	 *            要加密的值
	 * @return sha1加密后的值
	 */
	public static String sha1Hex(String value) {
		return DigestUtils.sha1Hex(value);
	}

	/**
	 * sha256加密
	 * 
	 * @param value
	 *            要加密的值
	 * @return sha256加密后的值
	 */
	public static String sha256Hex(String value) {
		return DigestUtils.sha256Hex(value);
	}
	
	public static boolean isNotNull(Object str) {
		boolean app = true;
		String tmp;
		tmp = Object2String(str);
		if (str == null || "".equals(tmp) || "null".equals(tmp)) {
			app = false;
		}
		return app;
	}

	@SuppressWarnings("rawtypes")
	public static boolean ListIsNull(List s) {
		if (s == null)
			return true;
		if (s.size() <= 0)
			return true;
		return false;
	}

	public static String Object2String(Object t) {
		if (t == null)
			return "";
		if ("null".equals(String.valueOf(t)))
			return "";
		return String.valueOf(t);
	}

	/**
	 * 把null转换成空字符串
	 */
	public static String NulltoString(Object o) {
		if (o == null)
			return "";
		return o.toString();
	}

	/**
	 * 把空字符串转换成NULL
	 * 
	 * @param o
	 * @return
	 */
	public static String StringtoNull(Object o) {
		if (o.equals(""))
			return null;
		return o.toString();
	}

	/**
	 * 验证Email
	 * 
	 * @param email
	 */
	public static boolean validateEmail(String email) {
		String regex = "^(\\w-*\\.*)+@(\\w-?)+(\\.\\w{2,})+$";
		Pattern pattern = Pattern.compile(regex);
		Matcher matcher = pattern.matcher(email);
		return matcher.matches();
	}

	/**
	 * 验证手机号
	 * 
	 * @param phoneNum
	 */
	public static boolean validatePhone(String phoneNum) {
		Pattern pattern = Pattern.compile("^(13[0-9]|14[0-9]|15[0-9]|17[0-9]|(18[0-9]))\\d{8}$");
		// .compile("^((13[0-9])|(15[^4,\\D])|(18[0,5-9]))\\d{8}$");
		Matcher matcher = pattern.matcher(phoneNum);
		return matcher.matches();
	}

	/**
	 * chekc正则
	 */
	public static boolean match(String str, String pat) {
		Pattern pattern = Pattern.compile(pat);
		Matcher matcher = pattern.matcher(str);
		return matcher.matches();
	}

	public static String gbEncoding(final String gbString) {
		char[] utfBytes = gbString.toCharArray();
		String unicodeBytes = "";
		for (int byteIndex = 0; byteIndex < utfBytes.length; byteIndex++) {
			String hexB = Integer.toHexString(utfBytes[byteIndex]);
			if (hexB.length() <= 2) {
				hexB = "00" + hexB;
			}
			unicodeBytes = unicodeBytes + "\\u" + hexB;
		}
		// System.out.println("unicodeBytes is: " + unicodeBytes);
		return unicodeBytes;
	}

	public static String decodeUnicode(final String dataStr) {
		int start = 0;
		int end = 0;
		final StringBuffer buffer = new StringBuffer();
		while (start > -1) {
			end = dataStr.indexOf("\\u", start + 2);
			String charStr = "";
			if (end == -1) {
				charStr = dataStr.substring(start + 2, dataStr.length());
			} else {
				charStr = dataStr.substring(start + 2, end);
			}
			char letter = (char) Integer.parseInt(charStr, 16); // 16进制parse整形字符串。
			buffer.append(new Character(letter).toString());
			start = end;
		}
		return buffer.toString();
	}
	
	@Value("${ipad.user_url}")
	protected String oaUrl;
	
	
	public static String getOaUrl() {
		return PropertiesUtil.getProParm("ipad.user_url");
	}
	
	public static String getProParm(String parm) {
		return PropertiesUtil.getProParm(parm);
	}

}
